package bo.zhao.action.lamda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;

/*
 	使用lamda处理集合
 * */

public class F2 {

	public static class Person {
		private int id;
		private String name;
		private Double height;
		private String sex;
		
		public Person(int id, String name, Double height,String sex) {
			this.id=id;
			this.name=name;
			this.height=height;
			this.sex=sex;
		}
		
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public Double getHeight() {
			return height;
		}
		public void setHeight(Double height) {
			this.height = height;
		}

		public String getSex() {
			return sex;
		}

		public void setSex(String sex) {
			this.sex = sex;
		}
		 
	}
	
	public static List<Person> getPersonList(){
		
		List<Person> personList=new ArrayList<>();
		
		Person p1 = new Person(1, "赵二", 1.42,"M");
		Person p2 = new Person(2, "张三", 1.72,"F");
		Person p3 = new Person(3, "李四", 1.83,"M");
		Person p4 = new Person(4, "王五", 1.65,"F");
 
		personList.addAll(Arrays.asList(p1,p2,p3,p4));
		return personList;
	}
	
	public static void main(String[] args) { 

		List<Person> list=getPersonList();
		//循环遍历
		forEach(list);
		//转Map
		collectMap(list);
		
		//average
		collectAverage(list);
		
		//统计数量
		collectCount(list);
		
		//分组
		collectGroup(list);
		//
		collectJoin(list);
		//
		collectMapping(list);
		
	}
	
	/**
	 * 计算平均值
	 * */
	private static void collectAverage(List<Person> list) {
		
		//表达式返回简单值
		Double avg1=list.stream().collect(Collectors.averagingInt(p->p.id));
		System.out.println("collectAverage-1 : "+avg1);
		
		//表达式返回处理过的值
		Double avg2=list.stream().collect(Collectors.averagingInt(p->{
			int z=p.id+10;
			return z;
		}));
		System.out.println("collectAverage-2 : "+avg2);

		//表达式返回简单值
		Double avg3=list.stream().collect(Collectors.averagingDouble(p->p.height));
		System.out.println("collectAverage-3 : "+avg3);
		
	
 
	}
	
	
	private static void collectCount(List<Person> list) {
			
			//采集数量
			Long count=list.stream().collect(Collectors.counting());
			System.out.println("collectCount-1 : "+count);
			
			//过滤后再采集数量
			Long count2=list.stream().filter(p->{
				return p.id%2==0;
			}).collect(Collectors.counting());
			System.out.println("collectCount-2 : "+count2);
			
			
	}
	
	/**
	 * 分组
	 * */
	private static void collectGroup(List<Person> list) {
		//按性别分组
		Map<String,List<Person>> group1= list.stream().collect(Collectors.groupingBy(p->p.sex));
		System.out.println("collectGroup-1 : "+JSON.toJSONString(group1));
		
		Map<String,List<Person>> group2= list.stream().collect(Collectors.groupingBy(p->{return p.id+p.sex;}));
		System.out.println("collectGroup-2 : "+JSON.toJSONString(group2));
		
		
		
	}
	
	private static void collectGroupAdvance(List<Person> list) {
		// Collectors.groupingBy 还有两个方法，可以进一步研究
	}
	
	
	private static void collectMapping(List<Person> list) {

		//这里有提取属性集合的意思
		List<String> names1 = list.stream().collect(Collectors.mapping(Person::getName, Collectors.toList()));
		System.out.println("collectMapping-1 : "+JSON.toJSONString(names1));
		
	}
	
	private static void collectJoin(List<Person> list) {
 
		//这里用的 stream.map方法 有分化、转换的意思。
		String join1=list.stream().map(p->p.name).collect(Collectors.joining());
		System.out.println("collectJoin-2"+join1);
		
		String join2=list.stream().map(p->p.name).collect(Collectors.joining("] - [","P-[","]-S"));
		System.out.println("collectJoin-1"+join2);
	}
	
	
 
	private static void collectMap(List<Person> list) {
		Map<Integer,Person> map1= list.stream().collect(Collectors.toMap(Person::getId, a->a));
		System.out.println("toMap-1 : "+JSON.toJSONString(map1));
 
		Map<Integer,String> map2= list.stream().collect(Collectors.toMap(Person::getId, a->{ return a.name;}));
		System.out.println("toMap-2 : "+JSON.toJSONString(map2));
		
	}

	/**
	 * 循环遍历，逐个处理
	 * */
	private static void forEach(List<Person> list) {
		list.forEach(p->{
			//干点啥
			System.out.println("forEach "+p.name);
		});
	}

	 

}
